package com.iisigroup.scan.folder;

import java.io.File;
import java.io.IOException;
import java.io.InputStream; 
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * The Class ConfigInfo.
 */
public class ConfigInfo {
    /** The Constant logger. */
    private static final Logger LOGGER = LoggerFactory
            .getLogger(ConfigInfo.class);
    
    /** The Constant CONF. */
    private static final String CONF = "/config.properties";
    
    /** The Constant SCAN_FOLDER. */
    private static final String SCAN_FOLDER = "scanFolder";
    
    /** The Constant SONAR_PROJECT_FOLDER. */
    private static final String SONAR_PROJECT_FOLDER = "sonarProjectFolder";
    

    /**
     * Gets the conf map.
     *
     * @return the conf map
     */
    protected Map<String, String> getConfMap() {
        final Properties properties = new Properties();
        InputStream inStream = null;
        final Map<String, String> result = new HashMap<String, String>();
        try {
            inStream = getClass().getResourceAsStream(CONF);
            properties.load(inStream);
            final Set<Entry<Object, Object>> entrySet = properties.entrySet();
            for ( Entry<Object, Object> unit : entrySet ){
                if(unit.getKey()!=null && unit.getValue() !=null ){
                    result.put(unit.getKey().toString(),unit.getValue().toString());
                }
               
            }
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }finally{
            if(inStream != null ){
                try {
                    inStream.close();
                } catch (IOException e) {
                    LOGGER.error(e.getMessage(), e);
                }
            }
        }
        return result ;
    }

    /**
     * Gets the scan folder.
     *
     * @return the scan folder
     */
    public File getScanFolder() {
        final String path = getConfMap().get(SCAN_FOLDER);
        return new File(path);
    }

    /**
     * Gets the sonar project folder.
     *
     * @return the sonar project folder
     */
    public File getSonarProjectFolder() {
        final String path = getConfMap().get(SONAR_PROJECT_FOLDER);
        return new File(path);
    }

}
